package autocode

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
    autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
)

type BusNovelsService struct {
}

// CreateBusNovels 创建BusNovels记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsService *BusNovelsService) CreateBusNovels(busNovels autocode.BusNovels) (err error) {
	err = global.GVA_DB.Create(&busNovels).Error
	return err
}

// DeleteBusNovels 删除BusNovels记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsService *BusNovelsService)DeleteBusNovels(busNovels autocode.BusNovels) (err error) {
	err = global.GVA_DB.Delete(&busNovels).Error
	return err
}

// DeleteBusNovelsByIds 批量删除BusNovels记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsService *BusNovelsService)DeleteBusNovelsByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.BusNovels{},"id in ?",ids.Ids).Error
	return err
}

// UpdateBusNovels 更新BusNovels记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsService *BusNovelsService)UpdateBusNovels(busNovels autocode.BusNovels) (err error) {
	err = global.GVA_DB.Save(&busNovels).Error
	return err
}

// GetBusNovels 根据id获取BusNovels记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsService *BusNovelsService)GetBusNovels(id uint) (err error, busNovels autocode.BusNovels) {
	err = global.GVA_DB.Where("id = ?", id).First(&busNovels).Error
	return
}

// GetBusNovelsInfoList 分页获取BusNovels记录
// Author [piexlmax](https://github.com/piexlmax)
func (busNovelsService *BusNovelsService)GetBusNovelsInfoList(info autoCodeReq.BusNovelsSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&autocode.BusNovels{})
    var busNovelss []autocode.BusNovels
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.Uuid != "" {
        db = db.Where("Uuid = ?",info.Uuid)
    }
    if info.Name != "" {
        db = db.Where("name LIKE ?","%"+ info.Name+"%")
    }
    if info.Author != "" {
        db = db.Where("Author LIKE ?","%"+ info.Author+"%")
    }
    if info.MinorCate != "" {
        db = db.Where("MinorCate LIKE ?","%"+ info.MinorCate+"%")
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
	err = db.Limit(limit).Offset(offset).Find(&busNovelss).Error
	return err, busNovelss, total
}
